package demo1;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.util.HashMap;

/**
 * @Author liangzai
 * @Description:
 */
public class MessageThread implements Runnable{
    public Socket socket;

    public MessageThread(Socket socket){
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            InputStream inputStream = socket.getInputStream();
            DataInputStream dataInputStream = new DataInputStream(inputStream);
            String line = null;
            UserName userName = new UserName();
            while ((line = dataInputStream.readUTF()) != null) {
                //UserName userName = new UserName();
                System.out.println(userName.userName(socket.getInetAddress())+":"+line);
                //System.out.println(socket.getInetAddress()+":"+line);
                sendMsg(socket.getInetAddress()+":"+line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void sendMsg(String msg){
        for(Socket cilent:UserService.socketList){
            try {
                OutputStream outputStream = cilent.getOutputStream();
                DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
                dataOutputStream.writeUTF(msg);
                dataOutputStream.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

//    public String userName(InetAddress ip){
//        HashMap<String ,String> map = new HashMap<>();
//        map.put("/10.194.223.151","黄熙淋");
//        map.put("10.190.135.110","袁宇嘉");
//        map.put("10.194.218.218","刘鑫诚");
//        map.put("/10.194.119.247","陈韩磊");
//        //System.out.println(ip.toString());
//        //map.put(ip.toString(),"敖恺");
//        return map.get(ip.toString());
//    }

}
